<?php
//首页数据展示
require('env.php');
require('init.inc.php');
include_once 'functions.php';


if (isset($_GET['OrderProdDBId'])) {
    $OrderProdDBId = $_GET['OrderProdDBId'];
} else {
    $OrderProdDBId = '';
}


$index_std = ShopConfig::getConfig();
$index_images = ShopConfig::getDefProdImages();
$index_products = Products::getDefProduct();

//$index_products = [['product_id' => 2, 'title' => '手錶', 'sy_num' => '20', 'old_price' => '699', 'money_type' => 1, 'price' => '299', 'color' => '黑色', 'xinhao' => 'EL_BLACK', 'cp_img' => 'http://doqoaxp9o8r2n.cloudfront.net/uploads/pages/product/1113/1472139753_picture.jpg']];


$tongji1 = home_tongji(1);
$tongji2 = home_tongji(2);
$tongji3 = home_tongji(3);

//function escape2Html($str)
//{
//    if (strlen($str) == 0) return "";
//    $str = str_replace('/&quot;/', '"', $str);
//    $str = str_replace('/&lt;/', '<', $str);
//    $str = str_replace('/&gt;/', '>', $str);
//    $str = preg_replace('/&nbsp;/', ' ', $str);
//    $str = str_replace('/&amp;/', '&', $str);
//    $str = str_replace('/&#39;/', "'", $str);
//    return $str;
//}
//$tongji1 = ['tongji' => ''];
//$tongji2 = ['tongji' => ''];
//$tongji3 = ['tongji' => ''];

$online_service = OnService::online_service();

?>

<!DOCTYPE html>
<html>
<style>


    .registerform {
        margin-top: 30px;
        border: 1px solid;
        color: whitesmoke;
    }

    .img_full {
        height: auto;
        width: 100%;
    }

    .fb_chat {
        background-image: url(style/images/fbc.png);
        background-repeat: no-repeat;
        background-size: auto;
        background-position: 0 0px;
        margin-left: 50%;
        display: block;
        height: 40px;
        outline: none;
        overflow: hidden;
        white-space: nowrap;
        width: 40px;
    }

    .toolbar {
        text-align: center;
    }

    .btn-item {
        margin-top: 100px;
        width: 100%;
        height: 10%;
    }

    .btn-item-log {
        background: #FF6F20 url(style/images/btn-totop.png) 1px 18px no-repeat;
        background-size: 15px;
        width: 100%;
        position: fixed;
        bottom: 0px;
        height: 50px;
        line-height: 50px;
        display: block;
        border-left: 1px solid #FFF;
        text-align: center;
        font-size: 16px;
        color: #FFF;
        margin-left: 0px;
        font-weight: bold;
    }

    .btn-item-buy {
        background: #FF6F20;
        background-size: 15px;
        width: 100%;
        position: fixed;
        bottom: 0px;
        height: 50px;
        line-height: 50px;
        display: block;
        border-left: 1px solid #FFF;
        text-align: center;
        font-size: 16px;
        color: #FFF;
        margin-left: 20px;
        font-weight: bold;
    }
</style>
<?php include("header.php"); ?>

<body>
<div class="" id="top">
    <div class="container">
        <div class="container-desktop">
            <?php for ($i = 0; $i < count($index_images); $i++) { ?>
                <div class="editor-item" data-index="<?php echo $i ?>">
                    <img
                        src="<?php echo $index_images[$i] ?>"
                        style="width: 100%;">
                </div>
            <?php } ?>
        </div>
    </div>
</div>
<div class="fixed-badges">
    <div class="fixed-badge" id="fixed-cart">
        <i class="fa fa-shopping-cart" onclick="gocart()"></i>
        <i class="fa fa-spinner"></i>
    </div>
</div>
<a class="anchor-buyit" name="buy"></a>

<div id="cart-form">
    <div id="cart-container">
        <div class="container">
            <div class="row">
                <div class="col-md-8 col-md-offset-2">
                    <h2 class="center">《我要購買》</h2>
                    <ul class="list-unstyled list-products">

                    </ul>
                    <hr>
                    <ul class="list-unstyled list-products">
                        <?php foreach ($index_products as $key => $index_prod) { ?>
                            <li class="product" id="<?php echo $key + 1 ?>">
                                <div class="row">
                                    <div class="col-md-9">
                                        <a class="btn btn-md btn-danger btn-product "
                                           data-title="<?php echo $index_prod['products_name'] ?>"
                                           href="buy.php?pid=<?php echo $index_prod['products_id'] ?>"
                                           target="_self">加入購物車
                                        </a>
                                        <?php echo $index_prod['products_name'] ?>
                                        <strong>限量預購 最後<?php echo $index_prod['products_sale_other'] ?>件</strong>
                                        <small></small>
                                        <br>

                                        <div class="pricing">
                      <span class="price-original">
                        原價:
                        <del><?php echo $index_prod['products_price'] ?></del>
                      </span>
                                            <span
                                                class="font-danger">預購優惠 <?php echo $index_prod['products_special_price'] ?>
                                                免運費</span>


                                            <div>
                                            <span
                                                class="price-original">型号： <?php echo $index_prod['products_model'] ?></span>
                                            </div>
                                            <?php if (!empty($index_prod['products_size'])) { ?>
                                                <div>
                                            <span
                                                class="price-original">尺碼： <?php $sizes = explode("||", $index_prod['products_size']);
                                                $str = implode(",", $sizes);
                                                echo $str;
                                                ?></span>
                                                </div>
                                            <?php } ?>
                                        </div>
                                    </div>
                                    <div class="col-md-3" style="text-align: center;">
                                        <?php
                                        $small_img = $index_prod['products_small_images'];
                                        ?>
                                        <?php if ($small_img) { ?>
                                            <img width="156px" height="156px"
                                                 src="<?php echo $small_img ?>"
                                                 alt="Placeholder"/>
                                        <?php } ?>
                                    </div>
                                </div>
                                <div class="modal-template" style="display: none;">
                                    <div class="modal">
                                        <form class="form-loading" role="form" id="new_order_product"
                                              action="add_order.php" accept-charset="UTF-8" data-remote="true"
                                              method="post"><input name="utf8" type="hidden" value="&#x2713;"/>

                                            <div class="modal-dialog">
                                                <div class="modal-content">
                                                    <div class="modal-header">
                                                        <button aria-label="Close" class="close" data-dismiss="modal"
                                                                type="button"><i class="fa fa-times"></i></button>
                                                        <h4 class="modal-title"><?php echo $index_prod['products_name'] ?></h4>
                                                    </div>
                                                    <div class="modal-body">

                                                    </div>
                                                    <div class="modal-footer">
                                                        <input type="submit" name="commit" value="加入購物車"
                                                               class="btn btn-lg btn-block btn-primary btn-loading"
                                                               data-loading-text="正在加入購物車中 ..."/>
                                                    </div>
                                                </div>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </li>
                        <?php } ?>
                    </ul>
                    <hr>
                </div>
            </div>
        </div>

        <div class="container" id="buy_products">
            <div class="row">
                <div class="col-md-8 col-md-offset-2">

                    <?php


                    $order_products = getOrdersProds($_COOKIE['ansyuserid']);

                    if ($order_products) {
                        $totalPrice = 0;
                        foreach ($order_products as $ord) {
                            $totalPrice += ($ord['products_special_price'] * $ord['product_nums']);
                        }
                        ?>
                        <h2 class="center">目前我已選購 ...</h2>
                        <div class="area-loading" id="shopping-cart" style="display: block;">
                            <table class="table table-cart" data-allow-addtional-products="true">
                                <tbody>
                                <tr class="success">
                                    <th width="50"></th>
                                    <th>品項</th>
                                    <th class="right" width="100">單價</th>
                                    <th class="right" width="100">數量</th>
                                    <th class="right" width="100">總計</th>
                                </tr>
                                <?php foreach ($order_products as $order) { ?>
                                    <tr>
                                        <td class="center">
                                            <a class="btn btn-default" data-remote="true"
                                               onclick="delord('<?php echo $order['oid'] ?>')"><i
                                                    class="fa fa-times"></i></a>
                                        </td>
                                        <td>
                                            <?php echo $order['products_name'] ?>
                                        </td>
                                        <td class="right">
                                            <?php echo $order['products_special_price'] ?>
                                        </td>
                                        <td class="right">
                                            <?php echo $order['product_nums'] ?>
                                        </td>
                                        <td class="right"> <?php echo $order['products_special_price'] * $order['product_nums'] ?></td>
                                    </tr>
                                <?php } ?>
                                <tr>
                                    <td class="right" colspan="4">
                                        <strong>運費</strong>
                                    </td>
                                    <td class="right">
                                        <strong class="font-danger">
                                            免運
                                        </strong>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="right" colspan="4">
                                        <strong>總計</strong>
                                    </td>
                                    <td class="right">
                                        <strong> <?php echo $totalPrice ?> </strong>
                                    </td>
                                </tr>

                                </tbody>
                            </table>
                            <div id="form-coupon"></div>
                        </div>
                    <?php } else { ?>
                        <div class="area-loading" id="shopping-cart">
                            <div class="alert alert-danger center">目前尚無選購商品，請點選上方商品選購。</div>
                        </div>
                    <?php } ?>
                    <a class="btn btn-lg btn-danger btn-block btn-checkout btn-loading" data-loading-text="讀取中 ..."
                       onclick="checkProd()"><i class="fa fa-hand-pointer-o"></i>
                        立即結帳
                    </a>
                    <hr/>
                    <div class="row">
                        <h2 class="center">《訂單查詢》</h2>

                        <form id="form" class="registerform" target="_self" method="get"
                              action="finish.php" name="form">
                            <div style="text-align: left;">
                                <input
                                    style="width:70%;height: 40px;float: left;color: black;margin-left: 30px;margin-top: 20px"
                                    type="text" required
                                    name="order_search" value=""
                                    placeholder="請輸入訂單號，姓名或手機號" class="block input" id="order_search">
                                <input type="hidden" name="action" value="shop_search"/>
                            </div>
                            <div style="text-align:center;">
                                <button style="float:left;color: black;margin-left: 30px;margin-top: 20px" id="buy_now"
                                        class="c_txt orange_bg" type="submit">查詢
                                </button>
                            </div>
                            <br style="clear:both"/> <br style="clear:both"/>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<span class="STYLE1" style="color: #ff0000 !important;">
                	<div
                        style="text-align:center;width: 100%; padding: 10px 0;color: #ff0000;font-size: 14px;"><?php echo $index_std['store_footer']; ?>
                    </div>
                </span>

<div class="btn-item">
    <a class="btn-item-log" href="#top"></a>

    <a class="btn-item-buy" href="#cart-container">立即購買</a>

</div>
<div style="display:none;">
    <?php echo str_replace('&nbsp;', '', htmlspecialchars_decode($tongji1['tongji'], ENT_QUOTES)) . '<br/>' . str_replace('&nbsp;', '', htmlspecialchars_decode($tongji2['tongji'], ENT_QUOTES)) . '<br/>' . str_replace('&nbsp;', '', htmlspecialchars_decode($tongji3['tongji'], ENT_QUOTES)) . '<br/>'; ?>
</div>

<script type="text/javascript">


    function delord(oid) {
        if (!confirm("确定要刪除嗎？")) {
            return;
        }
        //数据库id
        $.ajax({
            url: 'order.php',// 跳转到 action
            data: {
                order_db_id: oid,
                isUpdate: 1
            },
            type: 'post',
            cache: false,
            dataType: 'json',
            success: function (data) {
                if (data.msg == "true") {
                    window.location.href = '<?php echo SERVER_ROOT;?>';
                } else {
                    alert("删除失败");
                }
            },
            error: function () {
                alert("删除失败！");
            }
        });
    }

    function gocart() {
        //判定下目前有沒有物品購物車裏面
        $.ajax({
            url: 'order.php',// 跳转到 action
            data: {
                isUpdate: -1
            },
            type: 'post',
            cache: false,
            dataType: 'json',
            success: function (data) {
                if (data.msg == "true") {
                    window.location.href = '<?php echo SERVER_ROOT?>' + 'shop_cart.php?OrderProdDBId=' + '<?php echo $OrderProdDBId?>';
                } else {
                    alert("當前購物車為空，請先選購商品");
                    return;
                }
            },
            error: function () {
                alert("服務器錯誤！");
            }
        });

    }


    function checkProd() {
        $.ajax({
            url: 'order.php',// 跳转到 action
            data: {
                isUpdate: -1
            },
            type: 'post',
            cache: false,
            dataType: 'json',
            success: function (data) {
                if (data.msg == "true") {
                    window.location.href = '<?php echo SERVER_ROOT?>' + 'invoicing_stp1.php?OrderProdDBId=' + '<?php echo $OrderProdDBId?>';
                } else {
                    alert("當前購物車為空，請先選購商品");
                    return;
                }
            },
            error: function () {
                alert("服務器錯誤！");
            }
        });

    }
</script>
</body>

<script>
    window.onload = function () {
        var username = 'ansyuserid';
        var uu_id = uuid();
        var value = 'u_' + uu_id;

        var cookie = getCookie(username);
        if (cookie == "") {
            setCookie(username, value)
        }

        browserRedirect();
    }

    function uuid() {
        var s = [];
        var hexDigits = "0123456789abcdef";
        for (var i = 0; i < 36; i++) {
            s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
        }
        s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
        s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
        s[8] = s[13] = s[18] = s[23] = "-";

        var uuid = s.join("");
        return uuid;
    }

    //JS操作cookies方法!
    //写cookies
    function setCookie(name, value) {
        var Days = 30;
        var exp = new Date();
        exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
        document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
    }

    function getCookie(cname) {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1);
            if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
        }
        return "";
    }


    function browserRedirect() {
        var sUserAgent = navigator.userAgent.toLowerCase();
        var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
        var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
        var bIsMidp = sUserAgent.match(/midp/i) == "midp";
        var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
        var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
        var bIsAndroid = sUserAgent.match(/android/i) == "android";
        var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
        var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
        if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
            $("div .container").removeClass("container");
            $("div .container-desktop").removeClass("container-desktop");
            $("div .container-desktop img").each(function () {
                $(this).addClass("img_full");
            });
        }
    }
</script>
</html>
